package cn.edu.zafu.bigdata.x.wupeng.i;

public class TheadDemo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread d = new Thread(new Dog());
        d.start();
        Thread w = new Thread(new Wash());
        w.start();
        Thread e = new Thread(new Eat());
        e.start();
        Thread w1 = new Thread(new Watch(w));
        w1.start();
    }
}
//写一个线程类，可以接受一个另外一个线程类作为参数，然后定时输出线程的状态
class Watch extends Thread{
    private Thread t;
    public Watch(Thread t){
        this.t = t;
    }
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t.getName()+"状态："+t.getState());
        }
    }
}


//写一个遛狗的线程
class Dog implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("遛狗:"+i);
        }
    }
}

class Wash implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5000; i++) {
            //System.out.println("洗衣服:"+i);
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Eat implements Runnable {
    @Override
    public void run() {
        System.out.println("准备做饭");
        Thread t1 = new MyThread2();
        t1.start();
//        try {
//            t1.join();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        Thread t2 = new Thread(new MyThread3());
        t2.start();
        try {
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("开始吃饭");
    }
}

class MyThread2 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            //System.out.println(i);
            System.out.println("备菜第"+(i+1)+"个.");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class MyThread3 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            //System.out.println(i);
            System.out.println("正在炒第"+(i+1)+"个菜.");;
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
